package com.liug.droidsmall2.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

//import com.common.zxing.CaptureActivity;
import com.bumptech.glide.Glide;
import com.common.zxing.CaptureActivity;
import com.liug.droidsmall2.MainActivity;
import com.liug.droidsmall2.R;
import com.liug.droidsmall2.adapter.HomeAdapter;
import com.liug.droidsmall2.base.BaseFragment;
import com.liug.droidsmall2.entities.HomeData;
import com.liug.droidsmall2.presenter.HomePresenter;
import com.liug.droidsmall2.utils.view.ToastUtil;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LG on 2018/7/31.
 */

public class HomeFragment extends BaseFragment {
    private RecyclerView recy_view;
    private HomePresenter homePresenter = new HomePresenter(this);
    private Context context;
    private SmartRefreshLayout refreshLayout;
    private ImageView to_top;
    private EditText et_serach;
    private LinearLayout serach;
    private RelativeLayout top_tab;
    private TextView top_tab3;
    private TextView top_tab2;
    private TextView top_tab1;
    private int[] distance;
    private RelativeLayout et_search_parent;
    private LinearLayout adving;
    List<HomeData.DataBean.GuanggaoBean> guanggao_parent = new ArrayList<>();
    private ImageView video_onclick;
    private ImageView sao_sao;
    private ImageView small_duack;

    @Override
    public void initData(Bundle savedInstanceState, Context context) {
        this.context = context;
        homePresenter.getHomeData();
        homePresenter.getWeatherData();
    }


    public void setData(HomeData.DataBean homeData) {
        if (homeData != null) {
            List<HomeData.DataBean.GuanggaoBean> guanggao = homeData.getGuanggao();
            guanggao_parent.addAll(guanggao);
            guanggao_parent.addAll(guanggao);
            guanggao_parent.addAll(guanggao);
            recy_view.setAdapter(new HomeAdapter(guanggao_parent, context, this));
        } else {
            ToastUtil.show(getActivity(), "加载出错");
        }
        refreshLayout.finishRefresh();
    }


    @Override
    public void initView(View view, final Context context) {
        recy_view = (RecyclerView) view.findViewById(R.id.recy_view);
        video_onclick = (ImageView) view.findViewById(R.id.video_onclick);
        to_top = (ImageView) view.findViewById(R.id.to_top);
        et_serach = (EditText) view.findViewById(R.id.et_serach);
        et_search_parent = (RelativeLayout) view.findViewById(R.id.et_search_parent);
        serach = (LinearLayout) view.findViewById(R.id.serach);
        adving = (LinearLayout) view.findViewById(R.id.adving);
        top_tab = (RelativeLayout) view.findViewById(R.id.top_tab);
        top_tab3 = (TextView) view.findViewById(R.id.top_tab3);
        top_tab2 = (TextView) view.findViewById(R.id.top_tab2);
        top_tab1 = (TextView) view.findViewById(R.id.top_tab1);
        sao_sao = (ImageView) view.findViewById(R.id.sao_sao);
        to_top.setVisibility(View.GONE);
        et_serach.setFocusable(false);
        recy_view.setLayoutManager(new LinearLayoutManager(context));
        refreshLayout = (SmartRefreshLayout) view.findViewById(R.id.reswsm);
        refreshLayout.setEnableLoadmore(true);
        refreshLayout.setRefreshHeader(new WaterDropHeader(getActivity()));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                homePresenter.getHomeData();
            }
        });

        to_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recy_view.scrollToPosition(0);
                to_top.setVisibility(View.GONE);
                top_tab3.setTextColor(Color.BLACK);
                top_tab2.setTextColor(Color.parseColor("#FE671E"));
                top_tab1.setTextColor(Color.BLACK);
                top_tab.getBackground().setAlpha(0);
                distance[0] = 0;
/*                Animation animation= AnimationUtils.loadAnimation(getActivity(), R.anim.simple_anim);
                et_serach.startAnimation(animation);*/

            }
        });


        video_onclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity activity = (MainActivity) getActivity();
                activity.openVideo();
            }
        });

        sao_sao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(getActivity(),CaptureActivity.class));

            }
        });

        final int maxDistance = 255;//当距离在[0,255]变化时，透明度在[0,255之间变化]
        //当距离在[0,255]变化时，透明度在[0,255之间变化]
        distance = new int[]{0};

        recy_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!recy_view.canScrollVertically(-1)) {
                    Log.e("55555", "=====到顶部");
                    to_top.setVisibility(View.GONE);
                    top_tab3.setTextColor(Color.BLACK);
                    top_tab2.setTextColor(Color.parseColor("#FE671E"));
                    top_tab1.setTextColor(Color.BLACK);
                } else {
                    Log.e("55555", "不到顶部");
                    to_top.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                distance[0] = distance[0] + dy;
                float percent = distance[0] * 0.8f / maxDistance;//百分比
                int alpha = (int) (percent * 255);
                Log.e("444444", "不到顶部" + alpha);
                setSystemBarAlpha(alpha);

                if (dy == 0) {
                    top_tab3.setTextColor(Color.BLACK);
                    top_tab2.setTextColor(Color.parseColor("#FE671E"));
                    top_tab1.setTextColor(Color.BLACK);
                } else {
                    top_tab3.setTextColor(Color.WHITE);
                    top_tab2.setTextColor(Color.WHITE);
                    top_tab1.setTextColor(Color.WHITE);
                }

                if (alpha < 130) {
                    et_search_parent.scrollTo(0, alpha);
                    adving.scrollTo(0, alpha - 135);
                } else {
                    et_search_parent.scrollTo(0, 130);
                    adving.scrollTo(0, 130 - 135);
                }

                // et_serach.setScaleX(dy);
            }
        });
    }


    /**
     * 设置标题栏背景透明度
     *
     * @param alpha 透明度
     */
    private void setSystemBarAlpha(int alpha) {
        if (alpha >= 255) {
            alpha = 255;
            top_tab.getBackground().setAlpha(alpha);
        } else {
            top_tab.getBackground().setAlpha(alpha);
        }
    }

    @Override
    public int setView() {
        return R.layout.home_fragment;
    }

}
